//
//  BLTagViewCell.m
//  BLButtonTagView
//
//  Created by yangsq on 2017/9/26.
//  Copyright © 2017年 yangsq. All rights reserved.
//

#import "BLTagViewCell.h"
#import "BLButtonTagView.h"

#define ScreenHeight [UIScreen mainScreen].bounds.size.height
#define ScreenWidth [UIScreen mainScreen].bounds.size.width




@interface BLTagViewCell ()

@property (nonatomic, strong) BLButtonTagView *tagView;
@end


@implementation BLTagViewCell

- (void)dealloc{
    _tagView = nil;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.contentView.backgroundColor = [UIColor clearColor];
        self.backgroundColor =[UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UIView * baseView = [[UIView alloc]init];
        baseView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:baseView];
        baseView.translatesAutoresizingMaskIntoConstraints = NO;
        NSLayoutConstraint *topBase = [NSLayoutConstraint constraintWithItem:baseView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTop multiplier:1 constant:0];
        
         NSLayoutConstraint *leftBase = [NSLayoutConstraint constraintWithItem:baseView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeft multiplier:1 constant:15];
        
        
         NSLayoutConstraint *bottomBase = [NSLayoutConstraint constraintWithItem:baseView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeBottom multiplier:1 constant:0];
        
         NSLayoutConstraint *rightBase = [NSLayoutConstraint constraintWithItem:baseView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeRight multiplier:1 constant:-15];
        
        [self.contentView addConstraints:@[topBase,leftBase,bottomBase,rightBase]];
        
        
        
        UIView * lineView = [[UIView alloc]init];
        lineView.frame = CGRectMake(14, 11.5, 4, 15);
        lineView.layer.cornerRadius = 2;
        lineView.layer.masksToBounds = YES;
        lineView.backgroundColor = [UIColor chColorWithHexString:@"#6C72F5"];
        [baseView addSubview:lineView];
        
        
        UILabel * titleLabel = [[UILabel alloc]init];
        titleLabel.frame = CGRectMake(CGRectGetMaxX(lineView.frame)+5 , 11.5, 200, 21);
        titleLabel.font = [UIFont systemFontOfSize:12];
        titleLabel.textColor = [UIColor chColorWithHexString:@"#6C72F5"];
        titleLabel.text = @"兴趣爱好类";
        titleLabel.middleY = lineView.middleY;
        [baseView addSubview:titleLabel];
        _titleLabel = titleLabel;
        
        _tagView = [[BLButtonTagView alloc]initWithTotalTagsNum:30
                                                      viewWidth:ScreenWidth-50
                                                        eachNum:0
                                                        Hmargin:Hmargin_VALUE
                                                        Vmargin:Vmargin_VALUE
                                                      tagHeight:Tag_Height
                                                         tagCor:Tag_Height/2
                                                    tagTextFont:[UIFont boldSystemFontOfSize:14]
                                                   tagTextColor:[UIColor chColorWithHexString:@"#6C72F5"]
                                           selectedTagTextColor:[UIColor whiteColor]
                                        selectedBackgroundColor:[UIColor chColorWithHexString:@"#6C72F5"]];
        _tagView.maxSelectNum = 10;
        MJWeakSelf;
        _tagView.selectBlock = ^(BLButtonTagView * _Nonnull tagView, NSArray * _Nonnull selectArray) {
            NSLog(@"%@",selectArray);
            [weakSelf handelArray:selectArray];
        };
        [baseView addSubview:_tagView];
        
        _tagView.translatesAutoresizingMaskIntoConstraints = NO;
        NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:_tagView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:baseView attribute:NSLayoutAttributeTop multiplier:1 constant:52.5];
        
         NSLayoutConstraint *left = [NSLayoutConstraint constraintWithItem:_tagView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:baseView attribute:NSLayoutAttributeLeft multiplier:1 constant:10];
        
        
         NSLayoutConstraint *bottom = [NSLayoutConstraint constraintWithItem:_tagView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:baseView attribute:NSLayoutAttributeBottom multiplier:1 constant:-10];
        
         NSLayoutConstraint *right = [NSLayoutConstraint constraintWithItem:_tagView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:baseView attribute:NSLayoutAttributeRight multiplier:1 constant:-10];
        
        [baseView addConstraints:@[top,left,bottom,right]];
        
    }
    return self;
}

-(void)handelArray:(NSArray *)array{
    if (_cellBlock) {
        _cellBlock(_index,array);
    }
}


- (void)setTextArray:(NSArray *)textArray row:(NSInteger)row{
//    if (row%2==0) {
        _tagView.eachNum = 0;
//    }else{
//        _tagView.eachNum = 3;
//    }
    _tagView.tagTexts = textArray;
}


+ (CGFloat)cellHeightTextArray:(NSArray *)textArray Row:(NSInteger)row{
    CGFloat height;
    NSInteger eachNum;
//    if (row%2==0) {
        eachNum = 0;
//    }else{
//        eachNum = 3;
//    }
    
    height = [BLButtonTagView returnViewHeightWithTagTexts:textArray
                                                 viewWidth:ScreenWidth-50
                                                   eachNum:eachNum
                                                   Hmargin:Hmargin_VALUE
                                                   Vmargin:Vmargin_VALUE
                                                 tagHeight:Tag_Height
                                               tagTextFont:[UIFont systemFontOfSize:14.f]];
    return height+10+52.5;
}





- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}





@end
